package cc.wanforme.fsync.client;

import cc.wanforme.fsync.entity.CopyLocalVo;
import cc.wanforme.fsync.entity.FileInfo;
import cc.wanforme.fsync.entity.FileInfoVo;
import cc.wanforme.fsync.entity.device.HeartBeat;
import cc.wanforme.st.server.util.HttpTool;
import cc.wanforme.st.server.vo.ResMessage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

@RestController
@RequestMapping("/client")
public class ClientController {


    private ClientCenter clientCenter;

    /** 心跳连接，获取client的设备信息 */
    @PostMapping("/beat")
    @ResponseBody
    public ResMessage<HeartBeat> beat() throws IOException {
        HeartBeat heartBeat = clientCenter.heartBeat();
        return ResMessage.success(heartBeat);
    }

    /** 检查路径下的文件夹，只需要一个 path。
     * path 为 null，表示所有根路径，
     * path 为 '/'， linux中表示根路径， windows 中表示java程序所在分区的根路径
     * */
    @PostMapping("/list")
    @ResponseBody
    public ResMessage<List<FileInfoVo>> listFiles(@RequestBody FileInfo info) {
        List<FileInfoVo> infos = FileInfoVo.listFileVos(info.getPath());
        return ResMessage.success(infos);
    }

    /** 新增文件 */
    @PostMapping("/add")
    @ResponseBody
    public ResMessage<Void> addFile(String path, Long modifyTime, MultipartHttpServletRequest request) throws IOException {
        MultipartFile file = request.getFile("file");
        clientCenter.saveFile( HttpTool.decParam(path),
                file, modifyTime,false, request);
        return ResMessage.success();
    }

    /** 覆盖文件 */
    @PostMapping("/update")
    @ResponseBody
    public ResMessage<Void> updateFile(String path, Long modifyTime, MultipartHttpServletRequest request) throws IOException {
        MultipartFile file = request.getFile("file");
        clientCenter.saveFile( HttpTool.decParam(path),
                file, modifyTime,true, request);
        return ResMessage.success();
    }

    /** 复制本地文件。源文件和目标文件都是当前 Client 下。
     * 复制直接覆盖 */
    @PostMapping("/copyLocal")
    @ResponseBody
    public ResMessage<Void> copyFile(@RequestBody CopyLocalVo vo) throws IOException {
        ClientCenter.copyLocal(vo.getSource(), vo.getTarget());
        return ResMessage.success();
    }

    /** 删除文件，只需要 path */
    @PostMapping("/del")
    @ResponseBody
    public ResMessage<Boolean> delFile(@RequestBody FileInfo info) {
        String path = info.getPath();
        boolean success = clientCenter.deleteFileOrFolder(path);
        return ResMessage.success(success);
    }

    /** 获取单个文件的信息 */
    @PostMapping("/info")
    @ResponseBody
    public ResMessage<FileInfoVo> fileInfo(@RequestBody FileInfo info) {
        String path = info.getPath();
        FileInfo fileInfo = FileInfo.from(new File(path));

        if(fileInfo!=null) {
            FileInfoVo vo = new FileInfoVo(fileInfo);
            return ResMessage.success(vo);
        } else {
            // data 为null则说明不存在
//            return ResMessage.fail("文件不存在");
            return ResMessage.success(null);
        }
    }

    @Autowired
    public void setClientCenter(ClientCenter clientCenter) {
        this.clientCenter = clientCenter;
    }

    public void printHeader(HttpServletRequest req) {
        System.out.println("getAuthType： " + req.getAuthType());
        System.out.println("getLocalAddr： " + req.getLocalAddr());
        System.out.println("getProtocol： " + req.getProtocol());
        System.out.println("getProtocolRequestId： " + req.getProtocolRequestId());
        System.out.println("getRemoteHost： " + req.getRemoteHost());
        System.out.println("getRemoteAddr： " + req.getRemoteAddr());
        System.out.println("getRemotePort： " + req.getRemotePort());


        Enumeration<String> names = req.getHeaderNames();
        while (names.hasMoreElements()) {
            String n = names.nextElement();
            System.out.println(n + ":" + req.getHeader(n));
        }
    }

}
